/*
 *  $Id: Abilities.cs 925 2009-12-02 12:50:02Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System;
using System.Collections.Generic;

using AgateLib.Geometry;

namespace Ragima {
    /// <summary>
    /// Содержимое таба, определяющее способности
    /// </summary>
    public class AbilitiesTabContent: TabContent {
        private Player _player;
        private AbstractObject _owner;
        private List<Region> _regions;
        private AbilityRegion _selected;

        public AbilitiesTabContent(Player player, AbstractObject owner): base(TabType.Abilities, false) {
            _player = player;
            _owner = owner;
            _regions = new List<Region>();
            for (int i = 0; i < Constants.FieldSize; i++)
                _regions.Add(new AbilityRegion(Constants.GetRowRect(i, 3), this, i));
        }

        public static AbilitiesTabContent GetContent(Tabs tabs, Player player, AbstractObject owner) {
            AbilitiesTabContent content = null;
            if (tabs != null) content = tabs.FindContent<AbilitiesTabContent>(delegate(AbilitiesTabContent other) {
                return other.Player == player && other.Owner == owner;
            });
            return content != null ? content : new AbilitiesTabContent(player, owner);
        }

        public Player Player { get { return _player; } }
        public AbstractObject Owner { get { return _owner; } }
        public bool IsEnemy { get { return _owner.Owner != _player; } }
        public List<AbilityCard> Abilities { get { return _owner.GetAbilities(); } }

        private TargetRequest _request = null;
        public AbilityRegion Selected {
            get { return _selected; }
            set {
                _selected = value;
                if (value != null) {
                    _request = new AbilityTargetRequest(value);
                    Game.Current.SetTarget(_request);
                } else if (Game.Current.HasTarget && _request == Game.Current.Target.Request) {
                    Game.Current.ResetTarget();
                    _request = null;
                }
                Game.Current.UpdateRegions();
            }
        }

        public override int Count {
            get { return Abilities.Count; }
        }

        public override IEnumerable<Region> GetRegions() {
            for (int i = 0; i < CountFromPosition; i++)
                yield return _regions[i];
        }

        public override Region SelectedRegion { get { return _selected; } }

        public override void OnSelect() {
            if (Game.Current.HasTarget && Game.Current.Target.IsRealTarget) return;
            Selected = null;
        }
        public override void OnDeselect() {
            if (Game.Current.HasTarget && Game.Current.Target.IsRealTarget) return;
            Selected = null;
        }
        public override void OnScroll() {
            Selected = null;
        }

        public AbilityCard GetAbility(int num) {
            int index = GetIndex(num);
            return (index >= 0 && index < Count) ? Abilities[index] : null;
        }
    }

    /// <summary>
    /// Регион, определяющий способность существа
    /// </summary>
    public class AbilityRegion: HelpRegion {
        private AbilitiesTabContent _content;
        private int _num;

        public AbilityRegion(Rectangle rect, AbilitiesTabContent content, int num): base(rect) {
            _content = content;
            _num = num;
        }

        public AbstractObject Owner { get { return _content.Owner; } }
        public AbilityCard Ability { get { return _content.GetAbility(_num); } }
        public bool Selected {
            get { return _content.Selected == this; }
            set { _content.Selected = value ? this : null; }
        }

        public bool CanUse(bool notice) {
            AbilityCard ability = Ability;
            if (ability == null || !_content.Player.TestCurrent(notice)) return false;
            return Owner.CanUseAbility(ability, notice);
        }

        public override void Draw() {
            AbilityCard ability = Ability;
            if (ability == null) return;
            ability.DrawSmall(Rect.X, Rect.Y);
            if (_content.IsEnemy) return;
            if (_content.Owner.IsUsedAbility(ability)) {
                Data.CardDelay.Color = Color.White;
                Data.CardDelay.Draw(Rect.Location);
            } else if (!CanUse(false))
                Data.CardDisabled.Draw(Rect.Location);
            if (Selected)
                Data.CardSelect2.Draw(Rect.Location);
        }

        public override void OnClick() {
            if (_content.IsEnemy) return;
            if (Selected) {
                Selected = false;
            } else {
                if (CanUse(true)) Selected = true;
            }
        }

        public override IHelpProvider HelpObject {
            get { return Ability; }
        }
    }
}